package com.core.dto;

import com.core.model.user.Privilege;
import com.core.model.user.Privileges;
import com.core.model.user.Role;
import org.apache.commons.collections.map.HashedMap;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: lxf
 * Date: 2016/6/27
 * Time: 17:20
 * To change this template use File | Settings | File Templates.
 */
public class RoleDTO {

    private String guid;
    private String name;
    private List<Privilege> privileges;
    private List<String> privilegeNames;

    public RoleDTO() {
    }

    public RoleDTO(String guid, String name, List<Privilege> privileges, List<String> privilegeNames) {
        this.guid = guid;
        this.name = name;
        this.privileges = privileges;
        this.privilegeNames = privilegeNames;
    }

    public static RoleDTO toDTO(Role role) {
        List<String> list = new ArrayList<>();
        for (Privilege p : role.getPrivileges()) {
            list.add(p.getLabel());
        }
        return new RoleDTO(role.guid(), role.getName(), role.getPrivileges(), list);
    }

    public static List<RoleDTO> toDTOs(List<Role> roles) {
        List<RoleDTO> list = new ArrayList<>();
        for (int i = 0; i < roles.size(); i++) {
            list.add(toDTO(roles.get(i)));
        }
        return list;
    }

    public static Role toRole(RoleDTO roleDTO) {
        return new Role(roleDTO.getName(), new Privileges(roleDTO.getPrivileges()));
    }

    public static Map<String, Object> toRoleMap(List<RoleDTO> roleDTOs) {
        Map<String, Object> map = new HashedMap();
        for (int i = 0; i < roleDTOs.size(); i++) {
            map.put(roleDTOs.get(i).getGuid(), roleDTOs.get(i).getName());
        }
        return map;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Privilege> getPrivileges() {
        return privileges;
    }

    public void setPrivileges(List<Privilege> privileges) {
        this.privileges = privileges;
    }

    public List<String> getPrivilegeNames() {
        return privilegeNames;
    }

    public void setPrivilegeNames(List<String> privilegeNames) {
        this.privilegeNames = privilegeNames;
    }

    public String getGuid() {
        return guid;
    }

    public void setGuid(String guid) {
        this.guid = guid;
    }
}
